<template>
  <div class="page">
    <Navbar title="我有疑问" />
    <div class="content">
      <div class="form-content">
        <el-form :model="ruleForm" label-width="100px" size="small" :rules="rules" ref="ruleForm">
          <el-form-item label="企业名称" prop="projectName">
            <el-input v-model="ruleForm.projectName" />
          </el-form-item>
          <el-form-item label="问题类型" prop="problemType">
            <el-select v-model="ruleForm.problemType">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="联系人" prop="contactName">
            <el-input v-model="ruleForm.contactName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="contactPhone">
            <el-input v-model="ruleForm.contactPhone" />
          </el-form-item>
          <el-form-item label="问题描述" prop="describes">
            <el-input v-model="ruleForm.describes" type="textarea" />
          </el-form-item>
          <el-form-item label="资料上传">
            <van-uploader v-model="fileList" :max-count="1" :after-read="afterRead" />
          </el-form-item>
        </el-form>
      </div>
      <el-button type="primary" style="width: 100%;margin-top: 15px" @click="onSave">确认提交</el-button>
    </div>
  </div>
</template>

<script>
import navbar from '@/mixins/navbar'
import { http_problem, uploadFile } from '@/api'
import { Toast } from 'vant'
import { problemType } from '@/eumn'
export default {
  name: 'Consulting',
  mixins: [navbar],
  data() {
    return {
      ruleForm: {
        projectName: '',
        problemType: '',
        contactName: '',
        contactPhone: '',
        describes: ''
      },
      options: problemType.options,
      fileList: [],
      fileUrl: '',
      rules: {
        projectName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        problemType: [
          { required: true, message: '请选择问题类型', trigger: 'blur' }
        ],
        contactName: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        describes: [
          { required: true, message: '请输入问题描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {

    afterRead(file) {
      file.status = 'uploading'
      file.message = '上传中...'
      uploadFile(file.file).then(res => {
        file.url = res.imgUrl
        file.status = 'success'
        this.fileUrl = [
          {
            fileName: res.fileName,
            fileType: res.suffix,
            fileUrl: res.imgUrl }
        ]
      }).catch((err) => {
        file.status = 'failed'
        file.message = '上传失败'
        Toast(err)
      })
    },
    onSave() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          http_problem.create({
            ...this.ruleForm,
            file: JSON.stringify(this.fileUrl)
          }).then(res => {
            this.ruleForm = {
              projectName: '',
              problemType: '',
              contactName: '',
              contactPhone: '',
              describes: ''
            }
            this.fileList = []
            Toast(res.msg)
            this.$router.back()
          })
        }
      })

    }
  }
}
</script>

<style scoped lang="scss">
.page {
    width: 100vw;
    overflow: hidden;
    .el-select,
    .el-input {
        width: 100%;
    }
    .content {
        padding: 23px 16px;
        .form-content {
            width: 100%;
            background: #FFFFFF;
            border-radius: 10px;
            padding: 27px 30px 27px 0;
        }
    }
}
</style>
